package PhysView;

use strict;
use CGI qw(:standard);

use Athena::WebAPI::HTML;
use Athena::WebAPI::Form;
use Athena::WebAPI::Table;
use Athena::WebAPI::View;

use Athena::NavHeaderView;
use Athena::NavFooterView;
use Athena::CellFormat qw(:all);

use base ("View");

sub new {
    # Get the class name
    my $stInvocant = shift(@_);
    my $stClass = ref($stInvocant) || $stInvocant;

    # Create the current instance and bless into class
    my $rSelf = { };
    bless($rSelf,$stClass);

    my %hParams = ( @_ );
    $rSelf->{params} = \%hParams;
    $rSelf->cellFormatInit();
    return $rSelf;
}

sub draw {
  my $rSelf = shift(@_);
  my $rParams = $rSelf->{params};

  my $rTable = Table->new(%{$rSelf->{params}->{table_style}});
  $rSelf->buildTable($rTable);

  my $rForm = Form->new(-method=>'post', -name=>'myForm');
  $rForm->addChild($rTable);
  $rForm->addHiddenFields($rParams->{'hidden_fields'});

  my $rHTML = HTML->new("athena web view");
  $rHTML->setBodyAttr(%{$rSelf->{params}->{body_style}});
  $rHTML->addChild($rForm);
  
  return $rHTML->draw();
}

sub buildTable {
   my $rSelf = shift(@_);
   my $rTable = shift(@_);
   my $iCurRow = 0;
   my $stTitle = "View Physicals / Farm Members";
   if ($rSelf->{params}->{action_name} eq "UpdatePhysAction") {
     $stTitle .= ' <font color="#800000">Update Mode</font>';
   }
   $rTable->addChild($iCurRow++, 0, NavHeaderView->new($rSelf->{params}));
   $rTable->addChild($iCurRow++, 0, $stTitle, -bgcolor=>"#ccccdd");
   my $rPhysTable = Table->new(-border=>'1', -cellpadding=>'3');
   my $key;
   my $iPCurRow = 0;
   my $iPCurCol = 0;
   my @arCols = 
     ('idx', 'host', 'on', 'up', 'in', 'mtime', 'dtime', 'cpu', 'net', 
      'ld', 'mem', 'disk', 'cus0', 'cus1', 'cus2', 'cus3', 'cus4', 'cus5',
      'cus6', 'cus7', 'cus8', 'cus9', 'farms');
   my @arColDefaultView = 
     ('host', 'on', 'up', 'in', 'cpu', 'ld', 'mem', 'cus1', 'cus2', 'cus3', 'farms');
   my %hDoCols;
   if (($rSelf->{params}->{show_cols}) 
       && (scalar (@{$rSelf->{params}->{show_cols}}) > 1)) {
       foreach $key (@{$rSelf->{params}->{show_cols}}) {
	 $hDoCols{$key} = 1;
       }
     }
   else {
     foreach $key (@arColDefaultView) {
       $hDoCols{$key} = 1;
     }
   }
   
   my $stColName;
   if ($rSelf->{params}->{options} ne "0") {
     $rPhysTable->addChild($iPCurRow++, 0, 
			   "Rename columns below. Only works if columns are chosen at bottom.", 
			   -bgcolor=>"#ccccdd",
			   -colspan=>scalar(keys(%hDoCols)));
     foreach $stColName (@arCols) {
       if (defined($hDoCols{$stColName})) {
	 my $stCurMvCol = $rSelf->_mvCol($stColName);
	 my $stRename = textfield(-name=>'mv_col.'.$stColName,
				  -size=>'5',
				  -maxlength=>'30',
				  -value=>"$stCurMvCol");
	 $rPhysTable->addChild($iPCurRow, $iPCurCol++, $stRename,
			       -bgcolor=>$rSelf->colColor());
       }
     }
   $iPCurRow++;
   }

   # this is done to preserve order as it appears in @arCols; 
   foreach $stColName (@arCols) {
     if (defined($hDoCols{$stColName})) {
       my $stDisplayCol = $rSelf->_mvCol($stColName);
       $rPhysTable->addChild($iPCurRow, $iPCurCol++, $stDisplayCol,
			     -bgcolor=>$rSelf->colColor());
     }
   }

   my $rPhyss = $rSelf->{params}->{physs};
   my $rFarms = $rSelf->{params}->{farms};

   my $key_by_name;

   foreach $key_by_name (sort(keys(%{$rPhyss->{idx_name}}))) {
     $key = $rPhyss->{idx_name}->{$key_by_name};
     # resets all color switches
     #$rSelf->cellColor(1);
     $rSelf->colColor(1);
     $rSelf->colTextColor(1,1);
     $iPCurRow++;
     $iPCurCol = 0;
     my $stCol;
     my $stColEl;
     # again, preserve order
     foreach $stCol (@arCols) {
       if (defined($hDoCols{$stCol})) {
	 if ($stCol eq "idx") {
	   $stColEl = "[$key]";
	 } elsif (($stCol eq "on") || ($stCol eq "up") || ($stCol eq "in")) {
	   if ($rSelf->{params}->{action_name} ne "UpdatePhysAction") {
	     $stColEl = $rPhyss->int_to_yn($rPhyss->{table}->{$key}->{$stCol});
	   } else {
	     $stColEl = checkbox(-name=>"idx.".$key.".".$stCol,
				 -checked=>$rPhyss->{table}->{$key}->{$stCol},
				 -value=>'1',
				 -label=>'',
				 -onClick=>"document.myForm.need_update.value = \"1\"");
	   }
	 } elsif ($stCol eq "farms") {
	   my @arFarms = split(/,/, $rPhyss->{table}->{$key}->{farms});
	   my $iFarm;
	   my $stFarms;
	   foreach $iFarm (@arFarms) {
	     if (scalar($iFarm) > -1) {
	       $stFarms .= $stFarms ? ", " : "";
	       $stFarms .= $rFarms->{table}->{$iFarm}->{name};
	     }
	   }
	   $stColEl = "<font size=-2>" . $stFarms . "</font>";
	 } else {
	   $stColEl = $rPhyss->{table}->{$key}->{$stCol};
	 }
	 $rPhysTable->addChild($iPCurRow, $iPCurCol++, 
			       $rSelf->colTextColor($stColEl) . "\n");
       }
       
     }
   }
   $rTable->addChild($iCurRow++, 0, $rPhysTable);
   if ($rSelf->{params}->{options} ne "0") {
     my $stChkBxs = checkbox_group(-name=>'show_cols',
				   -values=>\@arCols,
				   -default=>\@arColDefaultView,
				   -rows=>2);
     $rTable->addChild($iCurRow++, 0, "columns to show above<br>" . $stChkBxs, 
		       -bgcolor=>"#ccccdd");
     $rTable->addChild($iCurRow++, 0, "custom field labels", 
		       -bgcolor=>"#ccccdd");
     

   }
   my $stChkBxs = "";
   # options not available in update mode
   if ($rSelf->{params}->{action_name} ne "UpdatePhysAction") {
     $stChkBxs = checkbox(-name=>'options',
			  -checked=>0,
			  -value=>'1',
			  -label=>'show options');
   }
   $rTable->addChild($iCurRow++, 0, submit(-name=>'send') . " " . $stChkBxs, 
		     -bgcolor=>'#ddcccc');
   $rTable->addChild($iCurRow++, 0, NavFooterView->new($rSelf->{params}));

}

sub _mvCol {
  my $rSelf = shift(@_);
  my $stCol = shift(@_);
  if ($rSelf->{params}->{mv_cols} != "0") {
    if ($rSelf->{params}->{mv_cols}->{$stCol}) {
      my $stMvCol = $rSelf->{params}->{mv_cols}->{$stCol};
      return $stMvCol;
    }
  }
  return $stCol;
}

1;
